package com.zbkj.front.controller.assistant.service.impl;


import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.constants.AssistantConstants;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.assistant.AssistantMessage;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.result.AssistantResultCode;
import com.zbkj.front.controller.assistant.service.AssistantConversationService;
import com.zbkj.front.controller.assistant.service.AssistantMessageService;
import com.zbkj.service.assistant.model.request.AssistantMessageFeedbackRequest;
import com.zbkj.service.assistant.model.request.AssistantMessageUserSearchRequest;
import com.zbkj.service.assistant.model.request.AssistantMessageUserSendRequest;
import com.zbkj.service.assistant.model.vo.AssistantMessageVO;
import com.zbkj.service.assistant.service.AssistantMessageBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author keNan
* @description AssistantMessageServiceImpl 接口实现
* @date 2025-06-11
*/
@Slf4j
@Primary
@Service
@RequiredArgsConstructor
public class AssistantMessageServiceImpl extends AssistantMessageBaseServiceImpl implements AssistantMessageService {
    private final AssistantConversationService assistantConversationService;

    @Override
    @Transactional
    public void feedback(AssistantMessageFeedbackRequest request, Integer userId) {
        AssistantMessage dbMessage = super.getById(request.getMessageId());
        if (ObjUtil.isEmpty(dbMessage)
            || !AssistantConstants.Message.CONTENT_TYPE_COMMENT.equals(dbMessage.getContentType())
            || ObjUtil.isNotEmpty(dbMessage.getContent())
            || !dbMessage.getUserId().equals(userId)
        ) {
            throw new CrmebException(AssistantResultCode.MESSAGE_CANT_FEEDBACK);
        }

        // 修改消息中的评价
        super.lambdaUpdate()
            .eq(AssistantMessage::getConversationId, dbMessage.getConversationId())
            .eq(AssistantMessage::getContentType, AssistantConstants.Message.CONTENT_TYPE_COMMENT)
            .set(AssistantMessage::getContent, request.getContent())
            .update();

        // 更新会话中的评价
        assistantConversationService.updateFeedback(dbMessage.getConversationId(), request.getContent());

    }

    @Override
    public AssistantMessageVO sendByUser(AssistantMessageUserSendRequest request, Integer userId){
        return this.send(request, userId, true);
    }

    @Override
    public PageInfo<AssistantMessageVO> getUserMessageList(AssistantMessageUserSearchRequest request, PageParamRequest pageParamRequest, Integer userId) {
        Page<AssistantMessage> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        List<AssistantMessage> list = super.lambdaQuery()
            .eq(AssistantMessage::getUserId, userId)
            .orderByDesc(AssistantMessage::getCreateTime, AssistantMessage::getId)
            .list();

        List<AssistantMessageVO> vos = list.stream().map(x -> {
            super.checkUserReadMessage(x);
            AssistantMessageVO vo = new AssistantMessageVO();
            BeanUtils.copyProperties(x, vo);
            return vo;
        }).collect(Collectors.toList());
        return CommonPage.copyPageInfo(page, vos);
    }

    @Override
    public void read(Integer messageId) {
        AssistantMessage message = super.getById(messageId);
        if (ObjUtil.isNull(message))
            return;
        super.checkUserReadMessage(message);
    }

}

